package encdb

import (
	"fhe-search-cloud/g"
	"fmt"
	"sync"

	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

var (
	encDB     *sqlx.DB
	encDBOnce sync.Once
)

// GetEncDBConn 单例模式创建db连接
func GetEncDBConn() *sqlx.DB {
	encDBOnce.Do(func() {
		dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s",
			g.GetConfig().MysqlConfig.User,
			g.GetConfig().MysqlConfig.Password,
			g.GetConfig().MysqlConfig.Host,
			g.GetConfig().MysqlConfig.Port,
			g.GetConfig().MysqlConfig.DbName,
		)
		var err error
		encDB, err = sqlx.Connect("mysql", dsn)
		if err != nil {
			panic(err)
		}
	})
	return encDB
}
